package com.dl.mall.controller.user;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.fastjson.JSONObject;
import com.dl.mall.annotation.MethodInfo;
import com.dl.mall.config.satoken.StpUserUtil;
import com.dl.mall.entity.Page;
import com.dl.mall.entity.user.Order;
import com.dl.mall.entity.vo.CartVo;
import com.dl.mall.entity.vo.HistoryOrderVo;
import com.dl.mall.entity.vo.OrderCartVo;
import com.dl.mall.exception.NoInventoryException;
import com.dl.mall.service.user.OrderService;
import com.dl.mall.utils.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 订单类控制器
 * @author DL
 * @create 2022-04-13 13:59
 */
@RestController
@RequestMapping("/order")
@Api(tags="订单类controller")
public class OrderController {

    @Resource
    private OrderService orderService;

    @ApiOperation(value="生成订单",notes = "订单商品信息")
    @PostMapping("/generate")
    @SentinelResource(value = "seckill")
    public Result generateOrder(@RequestBody List<CartVo> cartVoList) {
        System.out.println("cartVoList = " + cartVoList);
        String userId = StpUserUtil.getLoginIdAsString();
        Order order = orderService.generateOrder(userId, cartVoList);
        if (order != null){
            return Result.ok("生成订单成功").data("orderId", order.getOrderId());
        }else {
            return Result.error("生成订单失败");
        }
    }

    @ApiOperation(value="根据 订单id 获得订单信息",notes = "orderId=订单id")
    @MethodInfo
    @GetMapping("/info")
    @SentinelResource(value = "seckill")
    public Result getOrderInfo(String orderId) {
        Order order = orderService.getOrderInfoById(orderId);
        List<OrderCartVo> orderCartVoList = orderService.getOrderCartVoById(orderId);
        if (orderCartVoList != null){
            return Result.ok("查询订单成功").data("order", order).data("orderCartVoList", orderCartVoList);
        }else {
            return Result.error("查询订单失败");
        }
    }


    @ApiOperation(value="根据 订单id 使用支付宝支付，返回string内容",notes = "orderId=订单id,addressId=地址id")
    @MethodInfo
    @GetMapping("/aliPay")
    @SentinelResource(value = "seckill")
    public Result aliPay(String orderId, String addressId) {
        String body = orderService.aliPay(orderId, addressId);
        if (body != null){
            return Result.ok("成功").data("body", body);
        }else {
            return Result.error("失败");
        }
    }

    @ApiOperation(value="根据 用户id 获得历史订单信息（订单 + 商品）",notes = "userId=用户id")
    @MethodInfo
    @PostMapping("/history")
    @SentinelResource(value = "limiting")
    public Result getHistoryOrderInfo(@RequestBody JSONObject jsonObject) {
        String userId = StpUserUtil.getLoginIdAsString();
        Page<HistoryOrderVo> page = orderService.getHistoryOrderInfo(userId, jsonObject);
        if (page != null){
            return Result.ok("查询历史订单成功").data("page", page);
        }else {
            return Result.error("查询历史订单失败");
        }
    }
}
